﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class AudioManager
{
    public const string MusicPath = "Audio/bg/";
    public const string SoundPath = "Audio/sound/";
    public static AudioSource _as_music = null;
    public static List<AudioSource> mSoundList = new List<AudioSource>();

    public static bool mIsMusicOn;
    public static bool mIsSoundOn;

    public static event Action<bool> Signal_MusicSetChanged;
    public static event Action<bool> Signal_SoundSetChanged;

    public static void InitData()
    {
        mIsMusicOn = PlayerPrefs.GetInt(Constant.LOCAL_MUSIC_ON_KEY) != -1;
        mIsSoundOn = PlayerPrefs.GetInt(Constant.LOCAL_SOUND_ON_KEY) != -1;
        if (Signal_MusicSetChanged != null)
            Signal_MusicSetChanged(mIsMusicOn);
        if (Signal_SoundSetChanged != null)
            Signal_SoundSetChanged(mIsSoundOn);
    }

    public static void SetMusicOn(bool bSet)
    {
        if (mIsMusicOn != bSet)
        {
            mIsMusicOn = bSet;
            if (Signal_MusicSetChanged != null)
                Signal_MusicSetChanged(bSet);
        }

        int val = bSet == true ? 1 : -1;
        PlayerPrefs.SetInt(Constant.LOCAL_MUSIC_ON_KEY, val);

        if (_as_music != null)
        {
            if (_as_music.isPlaying && !bSet)
            {
                _as_music.Stop();
            }
            else if (!_as_music.isPlaying && bSet)
            {
                _as_music.Play();
            }
        }
    }

    public static void SetSoundOn(bool bSet, bool isImmediately = false)
    {
        if (mIsSoundOn != bSet)
        {
            mIsSoundOn = bSet;
            if (Signal_SoundSetChanged != null)
                Signal_SoundSetChanged(bSet);
        }

        int val = bSet == true ? 1 : 0;
        PlayerPrefs.SetInt(Constant.LOCAL_SOUND_ON_KEY, val);

        if (isImmediately && !bSet)
        {
            for (int i = 0; i < mSoundList.Count; i++)
            {
                if (mSoundList[i].isPlaying)
                {
                    mSoundList[i].Stop();
                }
            }
        }
    }

    public static void PlayMusic(string name)
    {
        if (_as_music == null)
        {
            _as_music = Camera.main.gameObject.AddComponent<AudioSource>();
        }

        _as_music.clip = Resources.Load<AudioClip>(MusicPath + name);
        _as_music.loop = true;
        _as_music.volume = 0.5f;

        if (mIsMusicOn)
            _as_music.Play();
    }

    public static void PlaySound(string name)
    {
        if (!mIsSoundOn)
            return;

        AudioSource __as = null;
        for (int i = 0; i < mSoundList.Count; i++)
        {
            if (mSoundList[i].isPlaying == false)
            {
                __as = mSoundList[i];
                break;
            }
        }

        if (__as == null)
        {
            __as = Camera.main.gameObject.AddComponent<AudioSource>();
            mSoundList.Add(__as);
        }

        __as.clip = Resources.Load<AudioClip>(SoundPath + name);
        __as.loop = false;
        __as.Play();
    }
}
